package com.flight.trans.view.edit;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.ContainerBean.ContainerData;
import com.flight.trans.bean.TugBean.TugData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.EditTextWatcher;
import com.flight.trans.listener.InputCapLowerToUpper;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ToastUtils;

import java.util.List;

/**
 * 拆板操作
 */

public class CustomContainerEditorActivity extends BaseActivity {

    private TextView mTvContainerNoText ,mTvContainerWeightText ;
    private EditText mEtContainerNo,mEtContainerWeight;
    private static EditorCallbackListener mEditorCallback;
    private List<ContainerData> mContainerDataList;
    private static  ContainerData mContainerData ;
    private static boolean isContainer ;

    private List<TugData> mTugList ;

    @Override
    protected int setContentView() {
        return R.layout.editor_custom_container;
    }


    protected void initView() {
        mTvContainerNoText = findViewById(R.id.mTvContainerNoText);
        mTvContainerWeightText = findViewById(R.id.mTvContainerWeightText);
        mEtContainerNo = findViewById(R.id.mEtContainerNo);
        mEtContainerWeight = findViewById(R.id.mEtContainerWeight);
    }

    @Override
    protected void initData() {
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);

        mTvContainerNoText.setText(getString(isContainer ? R.string.text_uld_no : R.string.text_custom_no));
        mTvContainerWeightText.setText(getString(isContainer ? R.string.text_uld_weight : R.string.text_custom_weight));

        if(isContainer){
            if(mContainerData != null) {
                mEtContainerNo.setText(mContainerData.getCode());
                mEtContainerWeight.setText(String.valueOf(mContainerData.getWeight()));
            }else  {
                mEtContainerNo.setText("");
                mEtContainerWeight.setText("");
            }
        }

        if(isContainer)
            mEtContainerNo.setTransformationMethod(new InputCapLowerToUpper());

        openKeyBoard(mEtContainerNo);

        setEvent() ;
    }

    @Override
    protected void initConnect() {
        if(isContainer) {
            NetworkUtils.getContainerList(new OkGoBackListener(mActivity) {
                @Override
                public void onSuccess(Object body) {
                    mContainerDataList = (List<ContainerData>) body;
                }
            });
        }else {
            NetworkUtils.getTugList(new OkGoBackListener(mActivity){
                @Override
                public void onSuccess(Object body) {
                    mTugList = (List<TugData>) body;
                }
            } , 0);
        }
    }

    private void setEvent() {
        mEtContainerNo.addTextChangedListener(new EditTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(mEtContainerNo.getText().toString()) || mEtContainerNo.getText().toString().endsWith("."))
                    return;
            }
        });

        mEtContainerNo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_NEXT) {
                    String count = mEtContainerNo.getText().toString();

                    if(TextUtils.isEmpty(count)){
                        ToastUtils.showToast(R.string.toast_no_null);
                        return true;
                    }else {
                        mEtContainerWeight.requestFocus();

                        if(isContainer && mContainerDataList != null)
                            for (int i = 0; i < mContainerDataList.size(); i++) {
                                if(count.equals(mContainerDataList.get(i).getCode())) {
                                    mEtContainerWeight.setText(String.valueOf(mContainerDataList.get(i).getWeight()));
                                    mEtContainerWeight.setSelection(mEtContainerWeight.getText().toString().length());
                                }
                            }

                        return false;
                    }
                }
                return false;
            }
        });

        mEtContainerWeight.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE) {
                    String volume = mEtContainerNo.getText().toString();

                    if(TextUtils.isEmpty(volume) || Double.parseDouble(volume) == 0){
                        ToastUtils.showToast(R.string.toast_weight_null);
                        return true;
                    }else {
                        return clickFinish();
                    }
                }
                return false;
            }
        });
    }

    /**
     * 点击确定
     * @param view
     * @return
     */
    public void onClick(View view){
        clickFinish();
    }

    public boolean clickFinish(){
        String mContainerNo = mEtContainerNo.getText().toString();
        String mContainerWeight = mEtContainerWeight.getText().toString();

        if(TextUtils.isEmpty(mContainerNo)){
            ToastUtils.showToast(R.string.toast_no_null);
            return true;
        }else if(TextUtils.isEmpty(mContainerWeight) || Double.parseDouble(mContainerWeight) == 0){
            ToastUtils.showToast(R.string.toast_weight_null);
            return true;
        } else {
            if(!isContainer && mTugList != null){
                boolean isContains = false ;
                for (int i = 0; i < mTugList.size(); i++) {
                    if(mTugList.get(i).getCode().equals(mContainerNo)){
                        isContains = true ;
                    }
                }

                if(isContains){
                    ToastUtils.showToast(getString(R.string.toast_tug_double));
                    return true;
                }
            }


            mBundle = new Bundle();
            mBundle.putString(Constant.Number , CommonUtils.toLowerCase(ConstantInfo.TEMPLE_START + mEtContainerNo.getText().toString()));
            mBundle.putDouble(Constant.Weight , Double.parseDouble(mEtContainerWeight.getText().toString()));
            mBundle.putBoolean(Constant.INFO , mContainerData == null);
            mBundle.putBoolean(Constant.TurnType , isContainer);
            mEditorCallback.onSubmit(mBundle);
            onFinish();
            return false;
        }
    }

    /**
     * 打开页面
     * @param baseActivity
     * @param editorCallback
     */
    public static void openActivity(BaseActivity baseActivity, EditorCallbackListener editorCallback , boolean container){
        Intent intent = new Intent(baseActivity, CustomContainerEditorActivity.class);

        mEditorCallback = editorCallback;
        isContainer = container ;
        mContainerData = null ;

        baseActivity.turnActForResult(intent);
    }


    /**
     * 打开页面
     * @param baseActivity
     * @param editorCallback
     */
    public static void openActivity(BaseActivity baseActivity, EditorCallbackListener editorCallback , ContainerData containerData){
        Intent intent = new Intent(baseActivity, CustomContainerEditorActivity.class);

        mEditorCallback = editorCallback;
        isContainer = true ;
        mContainerData = containerData ;

        baseActivity.turnActForResult(intent);
    }

    public interface EditorCallbackListener {
        void onSubmit(Bundle bundle);
    }

    @Override
    protected void changeSystemUi() {
    }

    @Override
    protected void onDestroy() {
        mEditorCallback = null;
        super.onDestroy();
    }
}
